<template>
  <div class="wrap">
    <div v-for="(item,index) in Questions">
      <div class="bg_color"></div>
      <mt-radio v-model="checkArr[index]" v-bind:title="item.Question" :options="answers[index]">

          
      </mt-radio>
    </div>
    <mt-button @click="signUp()" type="primary">报名{{checkArr}}</mt-button>
  </div>
</template>
<script>
import { user_apply_api } from "src/servise/getData";
import { ApplyForActivity_api } from "src/servise/getData";
import { user_answer } from "src/servise/user_answer.js";
import Qs from 'Qs';
import { MessageBox } from 'mint-ui';
export default {

  data() {
    return {
      data: null,
      Questions: null,
      answers:[],
      checkArr:[],
      titleArr:[],
      ActivityID: sessionStorage._ActivityID
    }
  },
  methods:{

    signUp(){
      //回答问题后提交报名
      if(this.checkArr.length != this.titleArr.length){
        MessageBox.alert("存在未选择题目！");
        return;
      }
      
      //拼接 成 多维对象数据 当作参数返回
      var send_json = {};
      
      send_json["Questions"] = [];
      for(var i = 0; i < this.titleArr.length; i++){
        var QuestionID_ItemId = {}
        QuestionID_ItemId['QuestionID'] = this.titleArr[i];
        QuestionID_ItemId['ItemId'] = this.checkArr[i];
        send_json["Questions"].push(QuestionID_ItemId);
      }
      this.$http.post(ApplyForActivity_api, Qs.stringify({
        "ActivityID": sessionStorage._ActivityID,
        "OpenId": sessionStorage._LoginID,
        "sharerOpenId": "",
        "Token": "令牌都是假的",
        "appSecret": "appSecret",
        "Questions": send_json["Questions"]
      })
      ).then((res) => {

          console.log(res);
        if (res.data['ErrorCode'] != 0) {
           MessageBox.alert(res.data["ErrorMessage"]);
           return;
        }
        this.$router.push({ path: "/user_apply_succeed" });
      });

    },
    setAnswers(obj,That){
      //获取问题的答案
      
      for(var attr in obj){
        That.titleArr.push(obj[attr]["QuestionID"]);
       var arr = [];
       var i = 1;
        for(var ans in obj[attr]["QuestionItem"]){
          var json = {}
          json["label"] = obj[attr]["QuestionItem"][ans]["Item"];
          json["value"] = i;
          arr.push(json);
          i++;
        }
        That.answers.push(arr);
      }
    }
  },
  created() {
    let That = this;
    this.$http.post(user_apply_api, 
    Qs.stringify({
      ActivityID: That.ActivityID,
      "Token": "令牌都是假的",
      "appSecret": "appSecret"
    })).then((res) => {
    That.data = res.data;
    console.log(That.data);
    // if(That.data.Result == null){
    //   MessageBox.alert("活动问题不存在");
    //   //this.$router.go(-1);
    //   //return ;
    // }
    if(That.data.Result != null){
       // 如果存在活动问题 就 处理
       That.Questions = res.data.Result.Question;
       That.setAnswers(That.Questions,That);
    }
    // That.Questions = res.data.Result.Question;
    // // That.ActivityID = res.data.Result.ActivityID;

    // That.setAnswers(That.Questions,That);
    });
  }
}
</script>
<style scoped lang="stylus">
  body 
    background-color white
  .wrap
    font-size 0.3382rem /* 14px */
    .bg_color
      background-color #F2F2F2
      height 0.2415rem 
</style>

<style lang="stylus">
    .mint-cell:last-child
      background-image none
    .mint-button--normal
      width 100%
      position fixed
      bottom 0
      border-radius 0
   .mint-radiolist-title
      padding-left 0.3623rem /* 15px */
      height 0.9662rem  /* 40px */
      line-height 0.9662rem /* 40px */
      color #2A96F5
      background-color white
      font-size 0.3623rem /* 15px */
      margin: 0
      border-bottom #E5E5E5 1px solid
      .mint-radio-core
        width 0.2415rem
        height 0.2415rem
    .mint-cell-wrapper
      background-image none
      .mint-radio-label
        font-size 0.3382rem /* 14px */
    .mint-button-text
      font-size 0.4589rem
      height 1.2077rem
      line-height 1.2077rem
</style>


